<?php
namespace app\common\service;

use app\common\logic\CartLogic;
use app\common\logic\GoodsLogic;
use app\common\logic\wechat\WechatUtil;
use app\common\service\Base;
use think\facade\Db;

class Cart extends Base {

    /**
     * 添加购物车
     * @param goods_id
    */
    public function addCart(){

        $user_id = $this->user_id;

        $params = input();
        $data = [
            'user_id' => $user_id,
            'goods_id' => $params['goods_id'],
            'item_id' => $params['ggid'],
            'goods_num' => $params['num']??1,
        ];
        

        $cartLogic = new CartLogic($data);
        $cartLogic->setUserId($this->user_id);
        $result = $cartLogic->addGoodsToCart();
        //exit(json_encode($result));

        if($result['status']==1){
            $result = [
                'status' => true,
                'msg' => '',
                'data' => $result
            ];
        }else{
            $result = [
                'status' => false,
                'msg' =>  $result['msg']??'添加失败',
                'data' => ''
            ];
        }

        return $result;
    }
    //购物车列表
    public function cartList(){
        $cartLogic = new CartLogic();
        $cartLogic->setUserId($this->user_id);
        $cartList = $cartLogic->getCartList(0,0,1);//用户购物车
        if(isset($cartList['status']) && !$cartList['status']){
            $result = [
                'status' => false,
                'msg' => $cartList['msg'],
                'data' => []
            ];
            return $result;
        }
        $cartList = array_values($cartList);

        //推荐产品
        $goods_where = ['is_on_sale' => 1];
        $keywords = input('keyword');
        if($keywords!=''){
            $goods_where['goods_name'] = ['like','%'.$keywords.'%'];
        }
        //哪个专区
        $leixing_id = input('leixing_id')?:0;
        if($leixing_id){
            $goods_where['leixing_id'] = $leixing_id;
        }
        if(input('sid')){
            $goods_where['sid'] = input('sid');
        }
        if(input('cid')){
            $goods_where['cat_id'] = input('cid');
        }
        $order_by_str = 'goods_id desc';
        $data = [];
        $data['map'] = $goods_where;
        //猜您喜欢
        $data['field'] = 'goods_id,shop_price,level_limit,click_count,market_price,goods_name,goods_remark,leixing_id,score,zs_score,original_img';
        $data['order'] = $order_by_str;
        $data['user_id'] = $this->user_id;
        $tjdatalist = logic('GoodsLogic')->getListNew($data);
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $cartList,
            'tjdatalist' => $tjdatalist
        ];
        return $result;
    }
    /**
     * 删除购物车商品
     */
    public function cartDel(){
        $cart_ids = input('cart_ids');
        $sid = input('bid');
        $cartLogic = new CartLogic();
        if($sid>0){
            Db::name('cart')->where(['user_id'=>$this->user_id,'sid'=>$sid])->delete();
        }else{
            $cartLogic->setUserId($this->user_id);
            $result = $cartLogic->delete($cart_ids);
        }

        if($result !== false){
            $result = [
                'status' => true,
                'msg' => '删除成功',
                'data' => []
            ];
            return $result;
        }else{
            $result = [
                'status' => false,
                'msg' => '删除失败',
                'data' => []
            ];
            return $result;
        }
    }

    /**
     *  购物车加减
     */
    public function changeNum(){
        $cart_id = input('cart_id');
        $goods_num = input('goods_num');
        if (empty($cart_id) || empty($goods_num)) {
            $result = [
                'status' => false,
                'msg' => '请选择要更改的商品',
                'data' => []
            ];
            return $result;
        }
        $cartLogic = new CartLogic();
        $result = $cartLogic->changeNum($cart_id,$goods_num);
        $result = [
            'status' => true,
            'msg' => '修改成功',
            'data' => []
        ];
        return $result;
    }

}
